package com.cykj.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cykj.common.core.domain.R;
import com.cykj.common.core.utils.StringUtils;
import com.cykj.common.security.service.TokenService;
import com.cykj.common.security.utils.SecurityUtils;
import com.cykj.system.api.domain.SysUser;
import com.cykj.system.api.model.LoginUser;
import com.cykj.user.domain.Customer;
import com.cykj.user.mapper.CustomerMapper;
import com.cykj.user.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public R<String> register(Customer customer) {
        if (customerMapper.selectCount(new QueryWrapper<Customer>()
                .eq("telephone", customer.getTelephone())) > 0) {
            return R.fail("该手机号已注册");
        }
        // 密码加密
        customer.setPassword(SecurityUtils.encryptPassword(customer.getPassword()));
        customer.setCreateTime(new Date());
        
        // 设置默认值
        if (StringUtils.isEmpty(customer.getAvatar())) {
            customer.setAvatar("http://127.0.0.1:9300/statics/2025/default.jpg");
        }
        customer.setBalance(BigDecimal.ZERO);
        customer.setDefaultAddressId(1);
        customer.setStatus(1);
        customer.setName(customer.getTelephone());

        return customerMapper.insert(customer) > 0 
            ? R.ok("注册成功") 
            : R.fail("注册失败");
    }



    @Override
    public R<Map<String, Object>> login(Customer customer) {
        // 参数校验
        if (StringUtils.isEmpty(customer.getTelephone())) {
            return R.fail("手机号不能为空");
        }
        if (StringUtils.isEmpty(customer.getPassword())) {
            return R.fail("密码不能为空");
        }

        // 查询用户
        Customer dbCustomer = customerMapper.selectOne(new QueryWrapper<Customer>()
                .eq("telephone", customer.getTelephone()));

        if (dbCustomer == null) {
            return R.fail("用户不存在");
        }

        if (!SecurityUtils.matchesPassword(customer.getPassword(), dbCustomer.getPassword())) {
            return R.fail("密码错误");
        }

        if (dbCustomer.getStatus() != 1) {
            return R.fail("账号状态异常，请联系管理员");
        }

        // 构建LoginUser
        LoginUser loginUser = new LoginUser();
        SysUser sysUser = new SysUser();
        sysUser.setUserId(dbCustomer.getId().longValue());
        sysUser.setUserName(dbCustomer.getName());
        loginUser.setSysUser(sysUser);
        
        // 生成token
        Map<String, Object> tokenMap = tokenService.createToken(loginUser);
        tokenMap.put("userId", dbCustomer.getId());
        tokenMap.put("userName", dbCustomer.getName());
        return R.ok(tokenMap);
    }

    @Override
    public R<String> updateBlacklistStatus(Integer userId, Integer isBlacklisted) {
        return null;
    }

}